package com.rocker.graduation.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.rocker.graduation.dao.UserMapper;
import com.rocker.graduation.domain.User;
import com.rocker.graduation.domain.UserExample;
import com.rocker.graduation.entity.UserBean;
import com.rocker.graduation.service.IUserService;
import com.rocker.graduation.util.CipherUtils;

@Service
public class UserService implements IUserService {
	
	private static final Logger logger = Logger.getLogger(UserService.class);
	
	@Autowired
	private UserMapper userMapper;
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.impl.IUserService#checkUser(com.rocker.graduation.entity.UserBean)
	 */
	@Override
	public boolean checkUser(UserBean userBean){
		UserExample example = new UserExample();
		logger.info("According to the account type, use the corresponding method.");
		if (StringUtils.isEmpty(userBean.getUserName())) {
			example.createCriteria().andEmailEqualTo(userBean.getEmail());
			logger.info("Account is Email: " + userBean.getEmail());
		}else {
			example.createCriteria().andUserNameEqualTo(userBean.getUserName());
			logger.info("Account is Nickname: " + userBean.getUserName());
		}
		List<User> users = userMapper.selectByExample(example);
		if (CollectionUtils.isEmpty(users)) {
			logger.info("There is no record in db about this account");
			return false;
		}
		String password = CipherUtils.encodeByMD5(userBean.getPassword());
		if (!users.get(0).getPassword().equals(password)) {
			logger.info("The account and the password are dismatched!");
			return false;
		}
		userBean.setUserId(users.get(0).getUserId());
		userBean.setUserName(users.get(0).getUserName());
		logger.info("This account is effective!");
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.impl.IUserService#saveUser(com.rocker.graduation.entity.UserBean)
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveUser(UserBean userBean){
		User user = new User();
		user.setEmail(userBean.getEmail());
		user.setPassword(CipherUtils.encodeByMD5(userBean.getPassword()));
		user.setUserName(userBean.getUserName());
		int count = userMapper.insertSelective(user);
		if (count <= 0) {
			return false;
		}
		userBean.setUserId(user.getUserId());
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.rocker.graduation.service.impl.IUserService#isEngaged(java.lang.String, java.lang.Integer)
	 */
	@Override
	public boolean isEngaged(String str, Integer type){
		UserExample example = new UserExample();
		if (type.intValue() == 1) {
			example.createCriteria().andUserNameEqualTo(str);
		}else {
			example.createCriteria().andEmailEqualTo(str);
		}
		
		List<User> users = userMapper.selectByExample(example);
		if (CollectionUtils.isEmpty(users)) {
			return false;
		}
		return true;
	}
}
